﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using mana.Web.UI.WebControls;

namespace TestWebSite.UI.WebControls
{
    public partial class GridViewExtensionsPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ActualizarDatos();
                CargarPaginasEnCombo();
            }
        }

        private void ActualizarDatos()
        {
            grid1.DataBind();
        }

        private void CargarPaginasEnCombo()
        {
            ddlPagina.Items.Clear();

            for (int i = 1; i <= grid1.PageCount; i++)
            {
                ddlPagina.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }

            ActualizarPaginaSeleccionadaEnCombo();
        }

        private void ActualizarPaginaSeleccionadaEnCombo()
        {
            ddlPagina.SelectedValue = (grid1.PageIndex + 1).ToString();
        }

        protected void ddlPagina_SelectedIndexChanged(object sender, EventArgs e)
        {
            grid1.SetPageIndex(int.Parse(ddlPagina.SelectedValue) - 1);
        }

        protected void grid1_DataBinding(object sender, EventArgs e)
        {
            List<Persona> personas = new List<Persona>();
            for (int i = 0; i < 50000; i++)
            {
                personas.Add(new Persona() { Nombre = "Nombre " + i, Apellido = "Apellido " + i, Nacimiento = DateTime.Now.AddDays(-i), Fuma = i % 2 == 0 });
            }

            grid1.DataSource = personas;

            grid1.ManageDataBindingEvent<Persona>();
        }
        
        protected void grid1_Sorting(object sender, GridViewSortEventArgs e)
        {
            grid1.ManageSortingEvent(e);
        }

        protected void grid1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grid1.ManagePageIndexChangingEvent(e);
        }

        protected void grid1_PageIndexChanged(object sender, EventArgs e)
        {
            ActualizarPaginaSeleccionadaEnCombo();
        }
    }
}